package br.com.pontov.pieces;
 
import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import br.com.pontov.PiecesManager;
import br.com.pontov.Vector2D;
 
public class Ball extends Entity {
    private Paint paint;
 
    private Vector2D pos;
    private Vector2D dir;
    private float speed;
    private PiecesManager pieces;
    private Bitmap image;
    private int cont;
    
    public Ball(Bitmap image, Vector2D pos, Vector2D dir, float speed, PiecesManager manager) {
        super();
    	
        this.image = image;
    	this.pos = pos;
        this.dir = dir;
        this.speed = speed;      
 
        paint = new Paint();
        paint.setColor(Color.RED);
        
        pieces = manager;
        
        //inicializa com 0
        cont = 0;
    }

	public void draw(Canvas canvas) {
        canvas.save();
        canvas.drawBitmap(image, pos.getX(),pos.getY(), paint);
        canvas.restore();
    }
 
    private void move() {
    	if(cont < 3){  
    		cont ++;
    		pos.plusMe(dir.multiply(speed));
    	}   
    	else
    		pos.plusMe(dir);
    }
 
    public void processAI() {
    	Rect bounds = new Rect((int) pos.getX() - 5, (int) pos.getY() - 5,
                (int) pos.getX() + 5, (int) pos.getY() + 5);
 
        for (Entity ent : pieces.getPieces()) {
            if (ent == this)
                continue;
            if (ent instanceof BlockableEntity)
                if (bounds.intersect(ent.getBounds())) {
                    Vector2D n = ((BlockableEntity) ent).getNormal();
                    // r = v-2 * v.dot(n) * n
                    dir = dir.minus(n.multiply(2).multiply(dir.dot(n)));
                }
        }
        move();
    }
}
